﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using YAPL.Language.Ast;
using YAPL.Language.SourceParser.Helpers;
using YAPL.Language.SourceParser.Helpers;
using YAPL.Util;

namespace YAPL.Language.SourceParser.XML.NodeParsers {
	public class XmlBinaryParser : XmlExpressionParser {
		public XmlBinaryParser(XmlInputParser parser) : base(parser) {}
		public override Expression ParseNode(XElement node) {
			var leftNode = node.Element(XmlNames.BinaryLeftExpression);
			var rightNode = node.Element(XmlNames.BinaryRightExpression);
			Require.NotNull(leftNode);
			Require.NotNull(rightNode);
			var leftExpr = Parser.ParseNode(leftNode.Elements().First());
			var rightExpr = Parser.ParseNode(rightNode.Elements().First());
			var op = node.Attribute(XmlNames.BinaryOperatorAttribute);
			Require.NotNull(op);
			return Expressions.Binary(leftExpr, rightExpr, OperatorHelper.ParseBinaryOperator(op.Value));
		}
	}
}
